package com.cositas.dulces.lp;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.cositas.dulces.lp.model.Product;
import com.cositas.dulces.lp.model.ShoppingCart;
import com.cositas.dulces.lp.model.ShoppingCartStatus;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

public abstract class ApplicationFacade {

	public static List<ShoppingCart> getFutureRequest(String fbid) {
		// TODO
		return null;
	}
	
	public static ShoppingCart getShoppingCart(Long id) {
		Objectify ofy = ObjectifyService.begin();
		return ofy.get(ShoppingCart.class,id);
	}
	
	public static void setProcessed(Long id) {
		Objectify ofy = ObjectifyService.begin();
		ShoppingCart sc = ofy.get(ShoppingCart.class,id);
		sc.pending = Boolean.FALSE;
		ofy.put(sc);
	}
	
	public static ShoppingCart getOpenShoppingCart(String fbid) {
		// TODOrefactorizar en uno solo
		Objectify ofy = ObjectifyService.begin();
		return getOpenShoppingCart(fbid, ofy);
	}
	
	
	public static ShoppingCart addProduct(String fbid, Product product) {
		Objectify ofy = ObjectifyService.begin();
		ShoppingCart shoppingCart = getOpenShoppingCart(fbid, ofy);
		shoppingCart.lastUpdated = new Date();
		if (shoppingCart.products == null) {
			shoppingCart.products = new Product[1];
			shoppingCart.products[0] = product;
		} else {
			for (Product prod : shoppingCart.products) {
				if (prod.product.equals(product.product)) {
					prod.quantity = prod.quantity + 1;
					ofy.put(shoppingCart);
					return shoppingCart;
				}
			}
			// si llego aca es porque no estaba
			Product newProd[] = new Product[shoppingCart.products.length + 1];
			System.arraycopy(shoppingCart.products, 0, newProd, 0, shoppingCart.products.length);
			newProd[newProd.length - 1] = product;
			shoppingCart.products = newProd;
		}
		ofy.put(shoppingCart);
		return shoppingCart;
	}
	
	public static ShoppingCart removeProduct(String fbid, String productName) {
		Objectify ofy = ObjectifyService.begin();
		ShoppingCart shoppingCart = getOpenShoppingCart(fbid, ofy);
		shoppingCart.lastUpdated = new Date();
		Product newProd[] = new Product[shoppingCart.products.length - 1];
		int idx = 0;
		for (Product old : shoppingCart.products) {
			if (!old.product.equals(productName)) {
				newProd[idx++] = old;
			}
		}
		shoppingCart.products = newProd;
		ofy.put(shoppingCart);
		return shoppingCart;
	}
	
	public static ShoppingCart increaseProductQuantity(String fbid, String productName) {
		Objectify ofy = ObjectifyService.begin();
		ShoppingCart shoppingCart = getOpenShoppingCart(fbid, ofy);
		shoppingCart.lastUpdated = new Date();
		for (Product prod : shoppingCart.products) {
			if (prod.product.equals(productName)) {
				prod.quantity = prod.quantity + 1;
				ofy.put(shoppingCart);
				return shoppingCart;
			}
		}
		return shoppingCart; // throw exception
	}
	
	public static ShoppingCart decreaseProductQuantity(String fbid, String productName) {
		Objectify ofy = ObjectifyService.begin();
		ShoppingCart shoppingCart = getOpenShoppingCart(fbid, ofy);
		shoppingCart.lastUpdated = new Date();
		for (Product prod : shoppingCart.products) {
			if (prod.product.equals(productName)) {
				prod.quantity = prod.quantity - 1;
				ofy.put(shoppingCart);
				return shoppingCart;
			}
		}
		return shoppingCart; // throw exception
	}
	
	public static void requestBudget(String fbid, String email, String phone, String address, Date fordate) {
		Objectify ofy = ObjectifyService.begin();
		ShoppingCart shoppingCart = getOpenShoppingCart(fbid, ofy);
		shoppingCart.email = email;
		shoppingCart.phone = phone;
		shoppingCart.address = address;
		shoppingCart.forDate = fordate;
		shoppingCart.status = ShoppingCartStatus.BUDGET_REQUEST.name();
		ofy.put(shoppingCart);
		// TODO send email
	}
	
	public static ShoppingCart placeOrder(String fbid, String email, String phone, String address, Date fordate) {
		Objectify ofy = ObjectifyService.begin();
		ShoppingCart shoppingCart = getOpenShoppingCart(fbid, ofy);
		shoppingCart.email = email;
		shoppingCart.phone = phone;
		shoppingCart.address = address;
		shoppingCart.forDate = fordate;
		shoppingCart.status = ShoppingCartStatus.ORDER_REQUEST.name();
		ofy.put(shoppingCart);
		// TODO send email
		return shoppingCart;
	}

	private static ShoppingCart getOpenShoppingCart(String fbid, Objectify ofy) {
		Query<ShoppingCart> iter = ofy.query(ShoppingCart.class).filter("fbuserid", fbid).filter("status", ShoppingCartStatus.PENDING.name());
		if (iter.count() > 1) {
			// throw exception
			return iter.get();
		} else {
			if (iter.count() == 1) {
				ShoppingCart shoppingCart = iter.get();
				Calendar cal = Calendar.getInstance();
				cal.setTime(shoppingCart.lastUpdated);
				cal.add(Calendar.DATE, 1);
				Calendar today = Calendar.getInstance();
				if (cal.before(today)) {
					shoppingCart.products = null;
					ofy.put(shoppingCart);
				}
				return shoppingCart;
			} else {
				ShoppingCart shoppingCart = new ShoppingCart();
				shoppingCart.fbuserid = fbid;
				shoppingCart.status = ShoppingCartStatus.PENDING.name();
				shoppingCart.lastUpdated = new Date();
				ofy.put(shoppingCart);
				return shoppingCart;
			}
		}
	}
	
	
}
